<?php

namespace app\api\controller;

use think\Db;

class Holiday
{
    public function index(){
        set_time_limit(0);
        // 获取当前年份
        $currentYear = 2024;

        // 循环生成未来10年的日历
        for ($year = $currentYear; $year <= $currentYear + 30; $year++) {
            // 输出年份


            // 判断是否为闰年
            if ($currentYear % 4 == 0 && ($currentYear % 100 != 0 || $currentYear % 400 == 0)) {
                // 闰年，2月份有29天
                $daysInMonth = array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
            } else {
                // 非闰年，2月份有28天
                $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
            }

            // 循环每月的天数
            for ($month = 1; $month <= 12; $month++) {
                // 输出月份和星期几的偏移量
//                echo date('N', strtotime("$year-$month-01")) . "<br>";
//
                // 获取该月每天的星期几
                for ($day = 1; $day <= $daysInMonth[$month - 1]; $day++) {
                    // 输出日期和星期几
                    $date =  date('Y-m-d 00:00:00', strtotime("$year-$month-$day"));
                    $weekday = date('N', strtotime("$year-$month-$day"));

                    echo "Day: $date, Weekday: $weekday<br>";
                    db('holiday')->insert(['ddatetime'=>$date,'type'=>($weekday>5)?3:0,'type_bak'=>($weekday>5)?3:0],true);
                }
                echo "<br>";
            }
            echo "<br>";
        }
    }

    public function holiday()
    {
        $apikey = '9bcf4f100875704de919d53107d2542f9671';
        for ($i = 202401; $i < 202413 ;$i++) {
            $url = "https://api.apihubs.cn/holiday/get?field=date,weekend,workday,holiday_overtime,holiday_recess&year=2024&month=$i&cn=1&size=31&api_key=$apikey";
            $content = file_get_contents($url);
            $contents = json_decode($content, true);
            $list = array_reverse($contents['data']['list']);
            foreach ($list as $v) {
                $date = $v['date'];
                $type = 0;//默认工作日

                if ($v['holiday_overtime'] > 10) $type = 2;//调休
                if ($v['weekend'] == 1 && $v['workday'] <> 1) $type = 3;//周末并且非工作日
                if ($v['holiday_recess'] == 1) $type = 1;//法定节假日

                Db::table('fa_holiday')->insert(['ddatetime' => $date, 'type' => $type, 'type_bak' => $type]);

            }
        }


    }

}